/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.lardocelar.dispositivo;

import br.com.lardocelar.core.Captador;
import br.com.lardocelar.core.IpUtil;
import br.com.lardocelar.core.ServicoSistema;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.sql.*;
import java.util.List;
import javax.media.NoPlayerException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author Leandro
 */
public class LarDoceLar {

    /**
     * @param args the command line arguments
     * @throws java.lang.InterruptedException
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws InterruptedException, IOException, MalformedURLException, LineUnavailableException, UnsupportedAudioFileException, NoPlayerException {
        ServicoSistema.Falar("Teste");
        testeTokenizer();
        //teste();
    }
    
    public static void Servidor(String[] args) throws InterruptedException, IOException {
        System.out.println("********** INICIALIZAÇÃO SITEMA **********");
        System.out.print("Carregando Servidores...");
        Thread.sleep(1000);   
        System.out.println("OK");
        System.out.print("Carregando Captadores...");
        List<Captador> captadores = Captador.listTodosCaptadores();
        System.out.println("OK");
        System.out.println("Reiniciando Captadores:");
        for (Captador captadore : captadores) {
            System.out.print("Reiniciando [" + IpUtil.parseIpaddressToString(captadore.getIpAddress()) + "]: ");
            try
            {
                captadore.reiniciar();
                System.out.println("OK");
            }
            catch(Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }
        System.out.println("Verificando Comunicação captadores:");
        for (Captador captadore : captadores) {
            System.out.print("Verificando [" + IpUtil.parseIpaddressToString(captadore.getIpAddress()) + "]: ");
            try
            {
                System.out.println(captadore.getComunicacao().getSituacaoComunicacao(captadore));
            }
            catch(Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }
        System.out.println("********** FINALIZAR SISTEMA **********");
        //System.in.read();
    }
 
    public static void testeTokenizer() throws IOException{
        StreamTokenizer tokenizer = new StreamTokenizer(new StringReader("Mary had 1 little lamb..."));
        while(tokenizer.nextToken() != StreamTokenizer.TT_EOF){

            if(tokenizer.ttype == StreamTokenizer.TT_WORD) {
                System.out.println(tokenizer.sval);
            } else if(tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
                System.out.println(tokenizer.nval);
            } else if(tokenizer.ttype == StreamTokenizer.TT_EOL) {
                System.out.println();
            }

        }
    }            
    //    The following application does the following:
    //
    //Loads the JDBC ODBC driver.
    //Opens a ODBC data source which opens the file myDB.mdb present in the application working directory.
    //Gets the Statement object for SQL execution.
    //Generates the name of a table with a random number generator.
    //Creates the table.
    //Enters 25 random entries into it.
    //Displays the content of the table.
    //Deletes or drops the table created.
    //Closes the Statement object and then closes the connection to the database.
    public static void teste()
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = 
              "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDB.mdb;";
            Connection conn = DriverManager.getConnection(database, "", "");
            Statement s = (Statement) conn.createStatement();
            
            // create a table
            String tableName = "myTable" + String.valueOf((int)(Math.random() * 1000.0));
            String createTable = "CREATE TABLE " + tableName + 
                                 " (id Integer, name Text(32))";
            s.execute(createTable); 
            
            // enter value into table
            for(int i=0; i<25; i++)
            {
              String addRow = "INSERT INTO " + tableName + " VALUES ( " + 
                     String.valueOf((int) (Math.random() * 32767)) + ", 'Text Value " + 
                     String.valueOf(Math.random()) + "')";
              s.execute(addRow);
            }
            
            // Fetch table
            String selTable = "SELECT * FROM " + tableName;
            s.execute(selTable);
            ResultSet rs = s.getResultSet();
            while((rs!=null) && (rs.next()))
            {
                System.out.println(rs.getString(1) + " : " + rs.getString(2));
            }
            
            // drop the table
            String dropTable = "DROP TABLE " + tableName;
            s.execute(dropTable);
            
            // close and cleanup
            s.close();
            conn.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
